home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / tsbat45.zip / BATRICKS.TXT < prev    next >
Text File  |  1994-05-31  |  35KB  |  934 lines

  1. Assorted Batch Tricks                                Mon 30-May-94
  2. =====================
  3.                                                All rights reserved
  4.                             Copyright (c) 1993, 1994 by Timo Salmi
  5.  
  6. ..................................................................
  7. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  8. Moderating at garbo.uwasa.fi anonymous FTP  archives  128.214.87.1
  9. Faculty of Accounting & Industrial Management; University of Vaasa
  10. Internet: ts@uwasa.fi   BBS +(358)-61-3170972; FIN-65101,  Finland
  11. ..................................................................
  12.  
  13.   ┌───────────────────────────────────────────────────────────┐
  14.   │ This file belongs to TSBAT*.ZIP. Please do not distribute │
  15.   │ this batricks.txt file separately!                        │
  16.   └───────────────────────────────────────────────────────────┘
  17.  
  18. Introduction
  19. ============
  20.  
  21. This file contains assorted batch tricks. Many, but not all, have
  22. been used in the TSBAT*.ZIP collection of batches. Likewise, there
  23. are some useful further tricks, not documented here, to be found in
  24. the TSBAT batches. Furthermore, many users have sent me useful
  25. suggestions and their own alternative solutions. My best thanks for
  26. the material. You can find much of this feedback stored in the
  27. garbo.uwasa.fi:/pc/pd2/tspost*.zip files.
  28.  
  29. You are free to quote brief passages from this file provided you
  30. clearly indicate the source with a proper acknowledgment.
  31.  
  32. Comments and corrections are solicited. But if you wish to have
  33. individual batch programming consultation, please rather post your
  34. question to a UseNet newsgroup like comp.os.msdos.programmer. It is
  35. much more efficient than asking me by email. I'd like to help, but I
  36. am very pressed for time. I prefer to pick the questions I answer
  37. from the Usenet news. Thus I can answer publicly at one go if I
  38. happen to have an answer. Besides, newsgroups have a number of
  39. readers who might know a better or an alternative answer. Don't be
  40. discouraged, though, if you get a reply like this from me. I am
  41. always glad to hear from fellow batch file users.
  42.  
  43.  
  44. INDEX
  45. =====
  46.  
  47. 1)  Making "@echo off" general
  48. 2)  Deleting all files
  49. 3)  Nested loops
  50. 4)  Checking whether a directory exists
  51. 5)  Checking that a program is available at the current directory or at path
  52. 6)  Using subroutines in batches
  53. 7)  Convert a parameter to uppercase
  54. 8)  Appending a new directory to the path
  55. 9)  Comparing two files
  56. 10) Writing an empty line
  57. 11) Customizing the pause message
  58. 12) Complicate renaming with for
  59. 13) Checking for wildcards
  60. 14) Preventing breaking the batch
  61. 15) Prevent a break from bypassing your autoexec.bat
  62. 16) Getting the extension
  63. 17) The quote character %
  64. 18) Eliminating auxiliary batches
  65. 19) Utilizing the subst command in paths
  66. 20) How to run a batch once a week (testing for the weekday)
  67. 21) Testing if a file name includes a path
  68. 22) Showing the time without enter
  69. 23) Alternatives for testing for the errorlevel value
  70. 24) Redirecting a batch file's output
  71. 25) Testing for environment space sufficiency
  72. 26) A simple trick to "disable" a drive
  73. 27) Sending an escape sequence to the printer
  74. 28) Creating a random string
  75. 29) Finding out the length of a string
  76. 30) How to obtain the MsDos version into an environment variable
  77. 31) Finding out the number of regular files on a drive
  78. 32) Augmenting line numbers to a text file
  79. 33) Storing and returning to the original directory (push and pop)
  80. 34) Enticing the current date into an environment variable
  81. 35) Identifying the individual PC
  82. 36) For loop and redirection quirks
  83. 37) Traversing a directory tree
  84. 38) Echoing the redirection symbol
  85. 39) Getting the file basename
  86.  
  87.  
  88. 1. Making "@echo off" general
  89. =============================
  90.  
  91. If you want to turn the echo off, and do not wish to show that line
  92. on the screen, you can easily do this by applying
  93.  @echo off
  94.  
  95. There is a catch, however, because this only works since MsDos
  96. version 3.30.  So if you want to make it general, put the following
  97. line in your autoexec.bat file if you are using MsDos 3.30 or higher
  98.  set _echo=@
  99. Then use the following format in your batches, which will then work
  100. for any MsDos version
  101.  %_echo%echo off
  102.  
  103.  
  104. 2. Deleting all files
  105. =====================
  106.  
  107. One of the most Frequently Asked Questions (FAQs) about batches is
  108. how to suppress the "Are you sure (Y/N)?" confirmation requirement
  109. for del *.*.  Use the following:
  110.  echo y| del *.*
  111. If you wish to suppress the message too, use
  112.  echo y| del *.* > nul
  113. Whether or not it is sensible to suppress the confirmation can be
  114. debated, but this is the trick anyway.
  115.  
  116.  
  117. 3. Nested loops
  118. ===============
  119.  
  120. It is possible to have nested loops of a kind in batch programming.
  121. Consider the following two batches, and try it out by calling
  122. test.bat.
  123.   echo off
  124.   rem TEST.BAT
  125.   for %%f in (a b c d e f) do %comspec% /c test2 %%f
  126.  
  127.   echo off
  128.   rem TEST2.BAT
  129.   for %%g in (1 2 3) do echo %1%%g
  130.  
  131. Alternatively write everything below on a single line
  132.   for %%f in (a b c d e f) do %comspec% /c
  133.     for %%g in (1 2 3) do echo %%f%%g
  134. (The wrap has been used in the text is because of the right margin.
  135. Don't wrap your batch.).  The disadvantage of this alternative is
  136. that the echo will be on.
  137.  
  138.  
  139. 4. Checking whether a directory exists
  140. ======================================
  141.  
  142. It is sometimes useful to be able to test whether a particular
  143. directory exists. The following test is true if the %1 directory
  144. does not exist.
  145.  if not exist %1\nul if not exist %1nul echo Directory %1 does not exist
  146.  
  147.  
  148. 5. Checking that a program is available at the current directory or at path
  149. ===========================================================================
  150.  
  151. When you call a program from a batch, and do not give the explicit
  152. path to it, it is advisable to test that the program is available
  153. either at the current directory or the default path.
  154.   set _found=
  155.   if exist %1 set _found=yes
  156.   for %%d in (%path%) do if exist %%d\%1 set _found=yes
  157.   for %%d in (%path%) do if exist %%d%1 set _found=yes
  158.   if "%_found%"=="yes" goto _continue
  159.   echo %1 is not at path or the current directory
  160.   goto _out
  161.   :_continue
  162.   echo %1 found at path or in the current directory
  163.   :_out
  164.  
  165.  
  166. 6. Using subroutines and recursion in batches
  167. =============================================
  168.  
  169. It is possible to use subroutines within batches. The crucial trick
  170. is setting an environment variable (eg _return) to point to a label
  171. where to return after the subroutine has been performed. For an
  172. example see UNPACK.BAT, and BOOT.BAT, the sections :_common and
  173. :_subru.
  174.  
  175. Likewise it is possible to use recursion go emulate subroutines in
  176. batches. (Recursion means that a batch calls itself).
  177. As an example see SAFEDEL.BAT and trace the effects of the line
  178.  for %%f in (%1) do call safedel %%f recurse
  179. Note that safedel could be replaced by %0 because the zeroeth
  180. parameter of a batch file points to itself.
  181.  
  182.  
  183. 7. Convert a parameter to uppercase
  184. ===================================
  185.  
  186. This example shows how to ensure that the parameter %1 given to the
  187. batch is in uppercase. This utilizes the fact that MsDos converts
  188. the path to uppercase. The result is stored in upcase_ and then the
  189. original path is restored.
  190.   set tmp_=%path%
  191.   path=%1
  192.   set upcase_=%path%
  193.   path=%tmp_%
  194.   set tmp_=
  195.  
  196. The also is another method for getting case-independent results.
  197. This is adapted from Jeff Prosise's column in PC Computing, March
  198. 1993, pp. 216-217. If the batch below is called TEST.BAT, it makes
  199. no diffirence whether you enter "TEST yes" or "TEST YES" or "TEST
  200. yEs".
  201.   @echo off
  202.   if not "%1"=="" set %1=*****
  203.   set status_=
  204.   if "%yes%"=="*****" set status_=yes
  205.   if "%no%"=="*****" set status_=no
  206.   if not "%status_%"=="" echo The parameter %%1 was a %status_%
  207.   if "%status_%"=="" echo The parameter %%1 was neither a yes nor a no
  208.   if not "%1"=="" set %1=
  209.  
  210.  
  211. 8. Appending a new directory to the path
  212. ========================================
  213.  
  214. This often needed trick is basically very simple. For example
  215. to add direc